import http from '@ohos.net.http';
import ShopInfo from '../viewModel/ShopInfo'
import axios from '@ohos/axios'
class ShopModel {
  baseUrl: string = 'http://localhost:3000'
  pageNo: number = 1
  getShopList(): Promise<ShopInfo[]>
  {

    return new Promise((resolve, reject) => {
      let httpRequest = http.createHttp()
     axios.get
      (
         //this.baseUrl + '/shops?pageNo=' + this.pageNo + '&pageSize=3',
        this.baseUrl + '/shops',
       {
         params:{pageNo:this.pageNo,pageSize:3},
       }
      )
        .then(resp => {
          if (resp.status === 200) {
            console.log('查询成功:',JSON.stringify(resp.data))
            resolve(resp.data)

          } else {
            console.log('查询商铺失败,error:', JSON.stringify(resp))
            reject('查询商铺失败')

          }
        })
        .catch(error => {
          console.log('查询商铺信息失败，error:', JSON.stringify(error))
          reject('查询失败')
        })
    })
  }
  //基于http请求
  getShopList1(): Promise<ShopInfo[]>
  {
    return new Promise((resolve, reject) => {
      let httpRequest = http.createHttp()
      httpRequest.request
      (
         this.baseUrl + '/shops?pageNo=' + this.pageNo + '&pageSize=3',
       // 'http://localhost:3000/shops?pageNo=1&pageSize=3',
        {
          method: http.RequestMethod.GET
        }
      )

        .then(resp => {
          if (resp.responseCode === 200) {
            console.log('查询成功')
            resolve(JSON.parse(resp.result.toString()))

          } else {
            console.log('查询商铺失败,error:', JSON.stringify(resp))
            reject('查询商铺失败')

          }
        })

        .catch(error => {
          console.log('查询商铺信息失败，error:', JSON.stringify(error))
          reject('查询失败')
        })
    })


  }
}

const shopModel = new ShopModel()

export default shopModel as ShopModel
